package colin.project.sso.server.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import colin.project.sso.server.business.dao.SysAppDao;
import colin.project.sso.server.business.entity.SysApp;
import colin.project.sso.server.business.observer.enums.OperationEventEnums;
import colin.project.sso.server.business.service.AppService;
import colin.project.sso.server.business.service.bo.SysAppBo;
import colin.project.sso.server.common.constants.SsoConstants;
import colin.project.sso.server.common.event.annotation.OperationEvent;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("appService")
@Slf4j
public class AppServiceImpl extends CommonService implements AppService {

    @Autowired
    private SysAppDao sysAppDao;

    /**
     * @param isEnable 是否启用
     * @param idList   应用ID集合
     */
    public void enable(Boolean isEnable, List<Integer> idList) {
        if (idList.isEmpty()) {
            throw new IllegalArgumentException("无效的id集合");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("enable", isEnable ? SsoConstants.APP_ENABLE : SsoConstants.APP_UNENABLE);
        params.put("ids", idList);
        int updateResult = sysAppDao.getSQLManager().update("SysApp.updateEnableState", params);
        log.debug("更新app表enable状态,需更新{}条,结果更新{}条", idList.size(), updateResult);
    }

    /**
     * 保存
     *
     * @param t
     */
    public void save(SysAppBo t) {
        if (t == null) {
            throw new IllegalArgumentException("无效的app保存对象");
        }
        SysApp sysApp = new SysApp();
        BeanUtil.copyProperties(t, sysApp);
        if (sysApp.getId() != null) {
            sysApp.setId(null);
        }
        if (sysApp.getCreatetime() != null) {
            sysApp.setCreatetime(null);
        }
        sysAppDao.insert(sysApp);
    }

    /**
     * 根据状态查询结果-这个数据量不会太大,所以可以一次性查询
     *
     * @param isEnable 可用性
     * @return
     */
    public List<SysAppBo> findByAll(Boolean isEnable) {
        List<SysApp> sysApps = sysAppDao.createQuery().andEq("isenable", isEnable ? SsoConstants.APP_ENABLE : SsoConstants.APP_UNENABLE).select();
        List<SysAppBo> result = Convert.toList(SysAppBo.class, sysApps);
        return result;

    }

    /**
     * 分页查询
     *
     * @param name 应用名称
     * @param page
     * @return
     */
    public PageResult<SysAppBo> findPaginationByName(String name, Page page) {
        PageQuery<SysApp> pageQuery = new PageQuery<>();
        pageQuery.setPageNumber(page.getPageNumber());
        pageQuery.setPageSize(page.getPageSize());
        //根据业务设置排序
        //pageQuery.setOrderBy(page.getOrders());
        pageQuery.setPara("name", name);
        sysAppDao.templatePage(pageQuery);
        List<SysApp> sysApps = pageQuery.getList();
        List<SysAppBo> result = super.toList(SysAppBo.class, sysApps);
        PageResult pageResult = new PageResult((int) pageQuery.getPageNumber(),(int) pageQuery.getPageSize());
        pageResult.addAll(result);
        pageResult.setTotal((int) pageQuery.getTotalPage());
        return pageResult;
    }

    /**
     * @param code 应用编码
     * @return
     */
    public SysAppBo findByCode(String code) {
        List<SysApp> sysApps = sysAppDao.createQuery().andEq("code", code).select();
        //TODO 性能损耗点
        List<SysAppBo> result = super.toList(SysAppBo.class, sysApps);
        if (result != null) {
            return result.get(0);
        }
        return null;
    }


    /**
     * @param idList
     */
    @Transactional
    @OperationEvent(event = {OperationEventEnums.Permission_4_DeleteByAppIds, OperationEventEnums.RolePermission_4_DeleteByAppIds})
    public void deleteById(List<Integer> idList) {
        if (idList == null || idList.isEmpty()) {
            return;
        }
        sysAppDao.getSQLManager().update("SysApp.updateIdsForDelete", idList);
    }

}
